package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import butterknife.Bind;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.SkinListAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerSkinComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.SkinModule;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.SkinModel;
import com.zw.yzk.kabu.presenter.SkinListPresenter;
import com.zw.yzk.kabu.view.view.SkinListView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.loadmore.DefaultLoadMoreViewCreator;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by skydar on 2017/4/14.
 */

public class SkinListActivity extends BaseActivity implements SkinListView{

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.category_list)
    RecyclerView recyclerView;
    @Bind(R.id.empty)
    View empty;

    @Inject
    SkinListPresenter presenter;

    List<SkinModel> skinModels;
    DelegateAdapter delegateAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_skin_list;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        String title = getIntent().getStringExtra(ActivityConstant.SKIN_CATEGORY_SUBJECT);
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(title)
                .build();
    }

    @Override
    public void initComponent() {
        DaggerSkinComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .skinModule(new SkinModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化
        VirtualLayoutManager layoutManager = new VirtualLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //设置Adapter列表
        delegateAdapter = new DelegateAdapter(layoutManager);
        recyclerView.setAdapter(delegateAdapter);

        initRefreshLayout();
        getSkinList(0, 0);
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    private void initRefreshLayout() {
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreViewCreator(new DefaultLoadMoreViewCreator())
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getSkinList(0, 0);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

                        int count = skinModels.size();
                        getSkinList(count, skinModels.get(count - 1).getId());
                    }
                }).build();
    }

    private void refreshView(List<SkinModel> models){
        delegateAdapter.addAdapter(new SkinListAdapter(this, models));
    }

    @Override
    public void showSkinList(List<SkinModel> skinModels) {
        if (refreshLayout.isLoading()) {
            this.skinModels.addAll(skinModels);
            refreshLayout.loadMoreFinish(skinModels.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            this.skinModels = skinModels;
            delegateAdapter.clear();
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }

        refreshView(skinModels);
        refreshEmptyStatus(delegateAdapter.getItemCount() == 0);
    }

    public void getSkinList(int start, int lastId){
        presenter.getSkinList(start, lastId);
    }

    @Override
    public void fetchFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (presenter != null) {
            presenter.destroy();
        }
    }
}
